package org.lee.fm;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.Version;
import org.lee.fm.vo.User;

import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * FreeMarkerDemo
 *
 * @author dralee
 * @date 2021-12-28 18:44:36
 **/
public class FreeMarkerDemo2 {
    public static void main(String[] args) throws IOException {
        Configuration cfg = new Configuration(new Version("2.3.31"));
        cfg.setClassForTemplateLoading(FreeMarkerDemo2.class, "/");
        cfg.setDefaultEncoding("UTF-8");
        Template template = cfg.getTemplate("user.ftl");
        Map<String, Object> tempData = new HashMap<>();
        List<User> users = new ArrayList<>();
        users.add(User.builder().name("Kate").age(12).job("child").build());
        users.add(User.builder().name("Jim").age(22).job("student").build());
        users.add(User.builder().name("Tom").age(32).job("teacher").build());
        tempData.put("users", users);
        try (StringWriter sw = new StringWriter()) {
            template.process(tempData, sw);
            System.out.println(sw.getBuffer().toString());
            sw.flush();
        } catch (TemplateException e) {
            e.printStackTrace();
        }
    }
}
